﻿using GodSystemDAL;
using GodSystemModel;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GodSystemBLL
{
    public class PowersBLL
    {
        private static PowersModel powerModel = new PowersModel();
        private static PowerGroupModel powerGroup = new PowerGroupModel();


        public List<UserrolepowerModel> GetUserPowerByUserId(int id)
        {
            UserrolepowerModel userrolepowerModel = new UserrolepowerModel();
            userrolepowerModel.UserId = id;
            List<UserrolepowerModel> userrolepowerModels = new List<UserrolepowerModel>();
            PowersDAL powersDAL = new PowersDAL();
            MySqlDataReader userPowerList = powersDAL.GetUserPowerByUserId(userrolepowerModel);
            if (userPowerList.HasRows)
            {
                while (userPowerList.Read())
                {
                    UserrolepowerModel powerModel = new UserrolepowerModel();
                    powerModel.Id = userPowerList.GetInt32(0);
                    powerModel.PowerId = userPowerList.GetInt32(1);
                    powerModel.UserId = userPowerList.GetInt32(2);
                    userrolepowerModels.Add(powerModel);
                }
            }
            return userrolepowerModels;
        }

        

        public List<RolepowerModel> GetRolePower(int id)
        {
           
            RolepowerModel rolepowerModel = new RolepowerModel();
            rolepowerModel.Id = id;
            PowersDAL powersDAL = new PowersDAL();
            MySqlDataReader rolePowerList = powersDAL.GetRolepower(rolepowerModel);
            List<RolepowerModel> roleList = new List<RolepowerModel>();
            if (rolePowerList.HasRows)
            {
                while (rolePowerList.Read())
                {
                    RolepowerModel rp = new RolepowerModel();
                    rp.Id = rolePowerList.GetInt32(0);
                    rp.RoleId = rolePowerList.GetInt32(1);
                    rp.PowerId = rolePowerList.GetInt32(2);
                    
                    roleList.Add(rp);
                }
            }
            return roleList;
        }
        public int UpdatePower(int id,string remark,int groupId)
        {
            powerModel.Id = id;
            powerModel.Remark = remark;
            powerModel.PowerGroupId = groupId;
            PowersDAL powers = new  PowersDAL();
            return powers.UpdatePower(powerModel);
        }
        public object GetPowerGroup(string name)
        {
            PowersDAL powers = new PowersDAL();
            powerGroup.GroupName = name;
            return powers.GetPowerGroup(powerGroup);
        }

        public int AddPowerGroup(string name)
        {
            PowersDAL powers = new PowersDAL();
            powerGroup.GroupName = name;
            return powers.AddPowerGroup(powerGroup);
        }

        public List<PowerGroupModel> GetPowerGroup()
        {
            List<PowerGroupModel> list = new List<PowerGroupModel>();
            PowersDAL powers = new PowersDAL();
            MySqlDataReader powerList =  powers.GetPowerGroup();

            if (powerList.HasRows)
            {
                while (powerList.Read())
                {
                    PowerGroupModel p = new PowerGroupModel();
                    p.Id = powerList.GetInt32(0);
                    p.GroupName = powerList.GetString(1);
                    list.Add(p);
                }
            }
            return list;
        }

        public object GetPower(string node, string remark)
        {
            PowersDAL powers = new PowersDAL();
            PowersModel powersModel = new PowersModel();
            powersModel.Node = node;
            powersModel.Remark = remark;
            return powers.GetPower(powersModel);
        }

        public int AddPower(string node, string remark,int groupId)
        {
            PowersDAL powers = new PowersDAL();
            PowersModel powersModel = new PowersModel();
            powersModel.Node = node;
            powersModel.Remark = remark;
            powersModel.PowerGroupId = groupId;
            return powers.AddPower(powersModel);
        }

        public object GetPowerForName(string remark,int id)
        {
            powerModel.Id = id;
            powerModel.Remark = remark;
            PowersDAL powers = new PowersDAL();
            return powers.GetPowerByIdAndName(powerModel);
        }

        public List<PowersModel> GetPowersList()
        {
            List<PowersModel> powersData = new List<PowersModel>();
            PowersDAL powersDAL = new PowersDAL();
            MySqlDataReader powersList = powersDAL.GetAllPowers();
            if (powersList.HasRows)
            {
                while (powersList.Read())
                {
                    PowersModel p = new PowersModel();
                    p.Id = powersList.GetInt32(0);
                    p.Node = powersList.GetString(1);
                    p.Remark = powersList.GetString(2);
                    if(string.IsNullOrEmpty(powersList[3].ToString()))
                    {
                        p.PowerGroupId = 0;
                    }
                    else
                    {
                        p.PowerGroupId = powersList.GetInt32(3);
                    }
                    
                    powersData.Add(p);
                }
            }
            return powersData;
        }
        public int UpdateRolePower(List<int> ids, int roleId)
        {
            PowersDAL powersDAL = new PowersDAL();
            return powersDAL.UpdateRolePower(ids, roleId);
        }
    }
}
